'user strict'

const Controller = require("egg").Controller

class MainController extends Controller {
    async index() {
        this.ctx.body = "hi Admin"
    }
    async checkLogin() {
        let userName = this.ctx.request.body.userName
        let password = this.ctx.request.body.password
        let sql = "SELECT userName FROM admin_user WHERE userName = '" + userName + "' AND password='" + password + "'"
        const res = await this.app.mysql.query(sql)
        let data = {};
        if (res.length > 0) {
            data.resultCode = 1
            let openId = new Date().getTime()
            this.ctx.session.openId = openId
            data.openId = openId
            data.msg = "登录成功"
        } else {
            data.resultCode = 0;
            data.msg = "用户名或密码错误"
        }
        this.ctx.body = data
    }
    async getTypeInfo() {
        const resType = await this.app.mysql.select('type')
        if (resType.length > 0) {
            this.ctx.body = {
                resultCode: 1,
                data: resType
            }
        }

    }
    async addArticle() {
        let temp = this.ctx.request.body
        const result = await this.app.mysql.insert('article', temp)
        const insertSuccess = result.affectedRows === 1
        const insertId = result.insertId
        this.ctx.body = {
            insertId: insertId,
            isSuccess: insertSuccess
        }
    }
    async updateArticle() {
        let temp = this.ctx.request.body
        console.log("...............>",temp)
        const result = await this.app.mysql.update('article', temp)
        console.log("==========>",result)
        const updateSuccess = result.affectedRows === 1
        this.ctx.body = {
            isSuccess: updateSuccess
        }
    }
    async getArticleList(){
        let sql = "SELECT article.id as id,"+
            "article.title as title,"+
            "article.introduce as introduce,"+
            "article.view_count as view_count,"+
            "FROM_UNIXTIME(article.addTime,'%Y-%m-%d' ) as addTime,"+
            "type.typeName as typeName "+
            "FROM article LEFT JOIN type ON article.type_id = type.Id "+
            "ORDER BY article.id DESC "
        const res = await this.app.mysql.query(sql)
        // console.log(res)
        this.ctx.body={
            list:res
        }
    }
    async deleteArticle(){
        let id = this.ctx.params.id
        const res = await this.app.mysql.delete('article',{'id':id})
        if(res.affectedRows>0){
            this.ctx.body={
                resultCode:1,
                msg:`文章编号${id},删除成功`
            }
        }else{
            this.ctx.body={
                resultCode:0,
                msg:`文章编号${id},删除失败`
            }
        }
    }
    async getArticleById(){
        console.log(`------------------>`,this.ctx.request)
        let id = this.ctx.request.body.id
        console.log("-------------->",id,"---------------->openId",this.ctx.session.openId)
        let sql = "SELECT article.id as id,"+
            "article.title as title,"+
            "article.introduce as introduce,"+
            "article.article_content as article_content,"+
            "FROM_UNIXTIME(article.addTime,'%Y-%m-%d' ) as addTime,"+
            "article.view_count as view_count,"+
            "type.typeName as typeName,"+
            "type.id as typeId "+
            "FROM article LEFT JOIN type ON article.type_id=type.id "+
            "WHERE article.id="+id
        const res = await this.app.mysql.query(sql)
        if(res.length>0){
            this.ctx.body = {
                resultCode:1,
                msg:"查询成功",
                data:res
            }
        }else{
            this.ctx.body={
                resultCode:0,
                msg:"查询失败",
                data:[]
            }
        }
    }
}

module.exports = MainController